package com.woniuxy.closer.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.closer.ResponseEntity;
import com.woniuxy.closer.entity.TemperatureRecords;
import com.woniuxy.closer.service.TemperatureRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李玉桃
 * @since 2022-07-20
 */
@RestController
@RequestMapping("/closer/temperature-records")
public class TemperatureRecordsController {

    @Autowired
    TemperatureRecordsService temperatureRecordsService;

    @GetMapping("{id}")
    public ResponseEntity findById(@PathVariable int id){
        return new ResponseEntity(temperatureRecordsService.getById(id));
    }

    @PostMapping("")
    public ResponseEntity save(@RequestBody TemperatureRecords temperatureRecords){
        boolean flag = temperatureRecordsService.saveOrUpdate(temperatureRecords);
        if(flag){
            return ResponseEntity.SUCCESS;
        }else{
            return ResponseEntity.ERROR;
        }
    }

    @DeleteMapping("")
    public ResponseEntity delete(@PathVariable int id){
        boolean flag = temperatureRecordsService.removeById(id);
        if(flag){
            return ResponseEntity.SUCCESS;
        }else{
            return ResponseEntity.ERROR;
        }

}
    @PostMapping("find")
    public ResponseEntity findByCondition(@RequestBody Map map){
        QueryWrapper<TemperatureRecords> temperatureRecordsQueryWrapper = new QueryWrapper<>();
        Iterator iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry next = (Map.Entry) iterator.next();
            if (!next.getValue().equals("")) {
//                map.remove(next.getKey());
                temperatureRecordsQueryWrapper.like((String) next.getKey(), next.getValue());


            }
        }
        return new ResponseEntity(temperatureRecordsService.list(temperatureRecordsQueryWrapper));
    }



}

